package vn.conghuy.talkwithme.responsor.responsor;

import vn.conghuy.talkwithme.R;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class SearchResponsor extends Responsor {
	
	private static final String SEARCH_KEYWORD = "search";
	
	boolean isRequireSearchKey;
	
	public SearchResponsor(Context context, boolean isRequireSearchKey) {
		super(context);
		this.isRequireSearchKey = isRequireSearchKey;
	}
	
	@Override
	public String response(String request) {
		boolean isStartWithSearchKeyword = request.startsWith(SEARCH_KEYWORD);
		if((isRequireSearchKey == true && isStartWithSearchKeyword) || isRequireSearchKey == false){
			if(isStartWithSearchKeyword){
				request = request.substring(SEARCH_KEYWORD.length() + 1);
			}
			request = request.replace(" ", "+");
			Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/search?q="+request));
			context.startActivity(browserIntent);
			return context.getString(R.string.search_command);
		}
		return super.response(request);
	}

	@Override
	public int relativeWithLastRequest(String sentence) {
		return relativeLastResponse;
	}

}
